﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OrderTest;

namespace FormTest
{
    public partial class Form3 : Form
    {
        Order order;
        public Form3(Order order)
        {
            this.order = order;
        }
        public Form3()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form3_Load(object sender, EventArgs e)
        {
            if(order != null)
            {
                textBox1.Text = order.getOrderID();
                textBox2.Text = order.getClient();
                foreach(OrderDetails detail in order.getDetails())
                {
                    listBox1.Items.Add(detail.ToString());
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = listBox1.SelectedItem.ToString();
            if(str.Length != 0)
            {
                foreach(OrderDetails details in order.getDetails())
                {
                    if(str == details.ToString())
                    {
                        textBox3.Text = details.getProductName();
                        textBox4.Text = details.getAmount().ToString();
                        textBox5.Text = details.getPrice().ToString();
                    }
                }
            }
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string productName = textBox3.Text;
            int amount = Convert.ToInt32(textBox4.Text);
            double price = Convert.ToDouble(textBox5.Text);
            string str = listBox1.SelectedItem.ToString();
            if(str.Length != 0)
            {
                foreach(OrderDetails details in order.getDetails())
                {
                    if (str == details.ToString() && productName.Length != 0 && amount != 0 && price != 0 && productName == details.getProductName())
                    {
                        details.setProductName(productName);
                        details.setAmount(amount);
                        details.setPrice(price);
                        foreach (OrderDetails detail in order.getDetails())
                        {
                            double totalPrice = 0;
                            totalPrice += details.getTotalPrice();
                            order.setTotalPrice(totalPrice);
                        }
                    }
                    if(str == details.ToString() && productName.Length != 0 && amount != 0 && price != 0 && productName != details.getProductName())
                    {
                        OrderDetails det = new OrderDetails(price, amount, productName);
                        order.addDetails(det);
                    }
                    if (str == details.ToString() && (productName.Length == 0 || amount == 0 || price == 0))
                        order.deleteDetails(details);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text.Length != 0 && textBox2.Text.Length != 0)
            {
                order.setClient(textBox2.Text);
            }
        }
    }
}
